<?php
namespace app\admin\model;

use think\Db;
class ApiUserModel extends CommonModel
 {
  	private static $model;
	
	public function __construct()
	{
		self::$model = Db::name ('user');
	}

     /**
      * func  更新 or 添加
      */
     public function addOrUpdateApiUserInfo($data = [], $id = 0)
     {
         if (!$data) {
             return false;
         }
         if ($id) {
             $data = array_merge($data, ['update_time' => time()]);
             if (self::$model->where(array('id' => $id))->update($data)) {
                 return $id;
             } else {
                 return false;
             }
         } else {
             $data = array_merge($data, ['create_time' => time(), 'update_time' => time(), 'uuid' => uuid()]);
             if (self::$model->insert($data)) {
                 return self::$model->getLastInsID();
             } else {
                 return false;
             }
         }
     }

     /**
      * func 列表
      */
     public function getApiUserListInfo($condition = [], $orderArr = [], $pageArr = [], $fieldsArr = [], $joinArr = [])
     {
         return self::$model->alias('a')
             ->field($fieldsArr)
             ->where($condition)
             ->join($joinArr)
             ->order($orderArr)
             ->paginate($pageArr['limit'], false, ['page' => $pageArr['page']]);
     }

     /**
      * func 单条
      */
     public function getApiUserOneInfo($condition = [], $orderArr = [], $fieldsArr = [], $joinArr = [])
     {
         return self::$model->alias('a')
             ->field($fieldsArr)
             ->where($condition)
             ->join($joinArr)
             ->order($orderArr)
             ->find();
     }

     /**
      * func 总数
      */
     public function getApiUserCount($condition = [])
     {
         return (int)self::$model->alias('a')
             ->where($condition)
             ->count();
     }

     /**
      * func 求和
      */
     public function getApiUserSum($condition = [], $field = null)
     {
         return (float)self::$model->alias('a')
             ->where($condition)
             ->sum($field);
     }
 }